﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewLandOY20
{
    /// <summary>
    /// 静态配置文件数据读写方法
    /// </summary>
    static class MyConfigHelper
    {
        // 从配置文件读取索引
        public static string getConfigValue(string key)
        {
            try
            {
                string res = ConfigurationManager.AppSettings[key];
                return res;
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return null;
            }
        }

        // 写入配置文件
        public static bool setConfigValue(string key, string value)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings[key] != null)
                {
                    config.AppSettings.Settings[key].Value = value;
                }
                else
                {
                    config.AppSettings.Settings.Add(key, value);
                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return false;
            }
        }

    }
}
